

import each as each

from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By

from common.page import Page, IOS, XPATH
from time import sleep

from user_data import project_name


class StartPage(Page):

    username_input = (IOS, 'type == "XCUIElementTypeTextField"')
    password_input = (IOS, 'type == "XCUIElementTypeSecureTextField"')



    def login(self, username, password='123123'):

        clear_btn = self.find_buttons("清除")
        for each in clear_btn:
            each.click()
        try:
           self.find_element(*self.username_input).send_keys(username)
           self.find_element(*self.password_input).send_keys(password)
           self.find_button("登录").click()
           sleep(1)
           self.find_button("common login unSelected").click()
           self.find_button("登录").click()
           sleep(8)
        except NoSuchElementException:
        # except Exception:
           self.swipe_dr("up")
           sleep(3)

        for i in range(5):

            try:
                ele = self.find_static_text(string=project_name, contains_flag=5)
                ele.click()
                # self.find_static_text(string=project_name, contains_flag=5).click()
            except NoSuchElementException:
                self.swipe(40, 470, 40, 440)
                sleep(2)
                # self.find_static_text(string=project_name, contains_flag=1, times=5).click()
                continue
            else:
                break
        sleep(2)

           # # self.page.enter_project()
           # self.find_button("我").click()   #之前是直接退出，导致用例不执行，现在直接进项目
           # self.find_button("icon 设置copy").click()
           # sleep(1)
           # self.find_static_text("退出登录").click()
           # sleep(1)
           #   # self.find_xpath('//*[@name="退出登录"]')
           #   # self.__class__('XCUIElementTypeOther').click()
           # self.find_button("确定").click()


        # clear_btn = self.find_buttons("清除")
        # for each in clear_btn:
        #     each.click()
        # self.find_element(*self.username_input).send_keys(username)
        # self.find_element(*self.password_input).send_keys(password)
        # self.find_button("登录").click()
        # sleep(1)

